package org.vectrics.recruiter.struts.admin;

import java.util.Iterator;
import java.util.List;

import org.apache.ecs.html.A;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.common.util.DateUtil;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.document.CandidateDocument;
import org.vectrics.struts.PageListTag;
import org.vectrics.user.Person;

public class CandidateDocumentsTag extends PageListTag {

	private static final long serialVersionUID = 5152755161350673265L;

	private String personId = null;
	
	private String showRemove = null;
	
	protected String getDescription() {
		return null;
	}

    protected String getPageListLink() {
   		return(getContextPath() + "/admin/user/docs/list.do");
    }

	protected void showList(String description, Cell mainCell, List elements, Form form) {
		Table table = new BorderTable();
		table.setWidth("100%");
		mainCell.addElement(table);
		
		Row hdrRow = table.addRow();
        hdrRow.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
		
		//hdrRow.setDefaultCellClass("tableHdrBg");
		hdrRow.setDefaultCellFontClass("tableHdrFont");
		hdrRow.addCell().addText("Name");
		hdrRow.addCell().addText("Description");
		hdrRow.addCell().addText("Category");
		hdrRow.addCell().addText("Extension");
		hdrRow.addCell().addText("Saved File");
		hdrRow.addCell().addText("Created");
		hdrRow.addCell().addText("");
		if (this.isShowRemove()) {
			hdrRow.addCell().addText("");
		}

		Iterator iter = elements.iterator();
		while (iter.hasNext()) {
			CandidateDocument candidateDocument = (CandidateDocument)iter.next();
			Row row = table.addRow();
			row.setDefaultCellFontClass("subFont");

			Cell cell = row.addCell();
			cell.addText(candidateDocument.getName());
			
			cell = row.addCell();
			cell.addText(candidateDocument.getDescription());
			
			cell = row.addCell();
			cell.addText(candidateDocument.getCategory().getDescription(this.getLocale()));
			
			cell = row.addCell();
			cell.addText(candidateDocument.getFileExtension());

			cell = row.addCell();
			cell.addText(candidateDocument.getSavedFileName());

			cell = row.addCell();
			cell.addText(DateUtil.getFechaFormateada(candidateDocument.getDateCreated()));
			
			cell = row.addCell();

			A viewLink = new A();
			viewLink.setClass("subLink");
			//viewLink.setHref("/hrs-intranet/retrievedocument?documentId=" + candidateDocument.getId());
			viewLink.addElement("view");
            viewLink.setHref(this.getRequest().getContextPath() + "/retrievedocument/document." 
            		+ candidateDocument.getFileExtension() 
            		+ "?documentId=" + candidateDocument.getId()
            		+ "&userName=" + ThreadProperties.getPerson().getUser().getUserName()
            		+ "&password=" + ThreadProperties.getPerson().getUser().getPassword()
            		+ "&domainId=" + ThreadProperties.getDomain().getId().toString()
        			);
            viewLink.setOnClick("OpenLarge(this.href); return false");
            cell.addElement(viewLink);
            
            if (this.isShowRemove()) {
            	cell = row.addCell();
            	
            	A removeLink = new A();
            	removeLink.setClass("subLink");
            	removeLink.addElement("remove");
            	removeLink.setHref(this.getRequest().getContextPath() + "/admin/user/docs/remove.do" 
            			+ "?documentId=" + candidateDocument.getId().toString()
            			+ "&domainId=" + ThreadProperties.getDomain().getId().toString()
            			+ "&personId=" + personId
            	);
            	cell.addElement(removeLink);
            }
            
		}
	}

	protected PageList createPageList() {
		Person person = RecruitServiceLocator.getPersonService().findPersonById(new Long(this.getPersonId()));
		PageList pageList = new CandidateDocumentsPageList(person);
		return pageList;
	}

	public String getPersonId() {
		return personId;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
	}

	private boolean isShowRemove() {
		return "true".equalsIgnoreCase(this.getShowRemove());
	}

	/**
	 * @return the showRemove
	 */
	public String getShowRemove() {
		return showRemove;
	}

	/**
	 * @param showRemove the showRemove to set
	 */
	public void setShowRemove(String showRemove) {
		this.showRemove = showRemove;
	}
}
